package admin

import (
	"last/app/models"
	"last/pkg/database"
)

//var SiteModel = database.DB.Model(Sites{})

type Focus struct {
	models.BaseModel
	Title string `json:"title"`
	Img   string `json:"img"`
	models.CommonTimestampsField
}

// Store 新增
func (f *Focus) Store(data interface{}) bool {
	result := database.DB.Model(Focus{}).Create(data)
	return result.RowsAffected == 1
}

// Show 详情
func (f *Focus) Show(id string, data interface{}) error {
	return database.DB.Model(Focus{}).Where("id=?", id).First(&data).Error
}

// Remove 删除
func (f *Focus) Remove(id string, data interface{}) error {
	return database.DB.Model(Focus{}).Where("id=?", id).Delete(&data).Error
}

// Edit 更新
func (f *Focus) Edit(id int, data interface{}) error {
	return database.DB.Model(Focus{}).Where("id=?", id).Where("id=?", id).Updates(&data).Error
}

// List 列表
func (f *Focus) List(page, pageSize int, where map[string]interface{}) (error, int64, interface{}) {
	var total int64
	var results []Focus
	query := database.DB.Model(Focus{})
	if where["title"] != "" {
		query.Where("title=?", where["title"])
	}
	query.Count(&total)
	query.Offset((page - 1) * pageSize).
		Limit(pageSize)
	return query.Find(&results).Error, total, results
}
